const {userModel} =require('../model/user')
//获取用户信息列表接口
function headerGetUserList(req,res){

  // const{search,page,pagesize}=req.query
  
  userModel.find().then(result => {

    // console.log(result);

    // let arr =result.filter(item=>item.username.includes(search))

   

  //  用户排序
    result.sort(function(a,b){
    return  b.createTime.getTime()- a.createTime.getTime()
    })
    

    result.forEach(item =>{
      item.createTime=  item.createTime.getTime()
    })
    // console.log(arr);
   
    let obj={
      code:200,
      message: '请求列表成功',
      data:result
      // list: arr.slice((page - 1) * pagesize, page * pagesize)
    }

    res.send(obj)
  })
 
}
// 新增用户
function headerAddUser(req, res) {
    console.log(req.body)
    
   new userModel(req.body).save().then(result =>{
    if(result){
     res.send({code:200,message:'添加成功'})
    }
 })
}
// 修改用户信息
function UserUpdate(req, res) {
  console.log(req);
  const {_id,username,gender,age, cellphone,status,headerImg} =req.body

  userModel.findByIdAndUpdate(_id, { username, gender, age, cellphone, status, headerImg }).then(result => {
    console.log(result)
    
    res.send({
      code:200,
      message:'修改成功'
    })
  })
}

// 删除用户信息
function UserDelete(req,res){
  const {_id} =req.body
  userModel.findByIdAndDelete(_id).then(result => {
    
    res.send({
      code:200,
      message:'删除成功'
    })
  })
}

module.exports= {
  headerGetUserList,
  headerAddUser,
  UserDelete,
  UserUpdate
}